package ups.apps.converter;

import java.util.List;

import javax.el.ELContext;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import ups.apps.model.Cuenta;
import ups.apps.model.Usuario;

@FacesConverter("CuentaConverter")
public class CuentaConverter implements Converter {
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		List<Cuenta> ctas= this.getDAO();
		for(Cuenta c : ctas){
			if (c.getCueNumero().equals(value))
				return c;
		}
		return null;		
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
			
		return ((Cuenta) value).getCueNumero();
	}
	
	public List<Cuenta> getDAO(){				
		FacesContext ctx = FacesContext.getCurrentInstance();
		ELContext ec = ctx.getELContext();
		Application app = ctx.getApplication();
		return (List) app.evaluateExpressionGet(ctx,"#{transferBean.listCuentas}",List.class);		
	}
	
}
